﻿$(function(){
	function checkTime(){
		var now = new Date(CurentTime());
		if(now>endTime){
			alert('活动已经结束了，不能报名了，下次赶早吧');
			return false;
		}
		if(now>startTime){
			alert('活动已经开始不能报名了，下次赶早吧');
			return false;
		}
		return true;
	}

	function CurentTime()
    { 
        var now = new Date();
        var year = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day = now.getDate();            //日
        var hh = now.getHours();            //时
        var mm = now.getMinutes();          //分
        var clock = year + "/";
       
        if(month < 10)
            clock += "0";
       
        clock += month + "/";
       
        if(day < 10)
            clock += "0";
           
        clock += day + " ";
       
        if(hh < 10)
            clock += "0";
           
        clock += hh + ":";
        if (mm < 10) clock += '0'; 
        clock += mm+":00"; 
        return(clock); 
    } 
    function check_num(value){
	    //定义正则表达式部分
	    var reg = /^\d+$/;
	    return value.match(reg);
	}

	function trim(str){ //删除左右两端的空格
　　    return str.replace(/(^\s*)|(\s*$)/g, "");
　　}

	$('#btn_sign').click(function(){
		if(uid==''||uid==null){
			alert('请先登录吧，不然我不知道你是哪个哦');
		}else{
			if(!checkTime()){
				return;
			}else{
				$.ajax({
					url : signUrl,
					type : 'post',
					data : {actId: actId},
					dataType : 'json',
					success : function(r){
						alert(r.msg);
						if(r.success){
							totalSignNum++;
							signUsers=signUsers+', '+uname;
							if(signUsers.substring(0,1)===','){
								signUsers=signUsers.substring(1,signUsers.length);
							}
							$('#signUsers').html(signUsers+'</br> 共 '+totalSignNum+' 人');
							$('#span_signNum').html('已报名：<strong>'+totalSignNum+'</strong>人');
						}
					}
				});
			}
		}
	});

	$('#btn_signMore').click(function(){
		if(uid==''||uid==null){
			alert('请先登录吧，不然我不知道你是哪个哦');
		}else{
			if(!checkTime()){
				return;
			}
			$('#div_sign_more').toggle();
			$('#signName').val(uname+'(+1)');
		}
	});

	$('#dlg_btn_signMore').click(function(){
		var signName=$('#signName').val();
		var signNumStr=$('#signNum').val();
		var signNum=parseInt(signNumStr);
		if(trim(signName)==''){
			alert('请输入报名称呼');
			$('#signName').focus();
		}else if(signNumStr==''){
			alert('请输入报名人数');
			$('#signNum').focus();
		}else if(signNum<=0){
			alert('别调皮了，至少得有一个人吧');
		}else if(!check_num(signNumStr)){
			alert('调皮，报名人数只能输入数字');
		}else{
			$.ajax({
				url : signMoreUrl,
				type : 'post',
				data : {
						actId : actId,
						signName : signName,
						signNum : signNum,
						maxNum : maxNum,
						totalSignNum : totalSignNum
						},
				dataType : 'json',
				success : function(r){
					if(r.success){
						$('#div_sign_more').hide();
						alert(r.msg);
						signUsers=r.signUsers;
						totalSignNum=r.totalNum;
						$('#signUsers').html(r.signUsers+'</br> 共 '+totalSignNum+' 人');
						$('#span_signNum').html('已报名：<strong>'+totalSignNum+'</strong>人');
					}else{
						alert(r.msg);
					}
				}
			});
		}
		
	});
	$('#dlg_btn_signMore_undo').click(function(){
		$('#div_sign_more').hide();
	})

	$('#btn_undo').click(function(){
		if(uid==''||uid==null){
			alert('请先登录吧，不然我不知道你是哪个哦');
		}else{
			var now = new Date(CurentTime());
			if(now>endTime){
				alert('活动已经结束了，不能取消报名');
				return;
			}
			if(now>startTime){
				alert('活动已经开始了，不能取消报名');
				return;
			}
			$.ajax({
				url : undoUrl,
				type : 'post',
				data : {
					actId : actId
				},
				dataType : 'json' ,
				success : function(r){
					alert(r.msg);
					if(r.success){
						signUsers=r.signUsers;
						totalSignNum=r.totalNum;
						$('#signUsers').html(signUsers+'</br> 共 '+totalSignNum+' 人');
						$('#span_signNum').html('已报名：<strong>'+totalSignNum+'</strong>人');
					}
				}
			});
		}
	});
})